package com.amos.muexamcore.core;

import com.amos.constant.Constants;
import com.amos.core.BaseEntity;
import com.amos.core.PageDTO;
import com.amos.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamclient.service
 * @ClassName AbstractPageService
 * @Description TODO
 * @Author Amos
 * @Modifier
 * @Date 2020/3/4 22:53
 * @Version 1.0
 **/
public abstract class AbstractPageService<T extends PageDTO, E extends BaseEntity> extends AbstractService<T, E> implements BasePageService<T> {
    /**
     * 查询方法
     *
     * @param dto
     * @return
     */
    public abstract List<T> list(T dto);

    /**
     * 查询分页
     *
     * @param dto
     * @return
     */
    @Override
    public PageInfo<T> page(T dto) {
        int pageNum = StringUtils.isNullOrEmpty(dto)
                ? Constants.DEFAULT_PAGE_NUM : StringUtils.isNullOrEmpty(dto.getPageNum())
                    ? Constants.DEFAULT_PAGE_NUM: dto.getPageNum();
        int pageSize = StringUtils.isNullOrEmpty(dto)
                ? Constants.DEFAULT_PAGE_SIZE : StringUtils.isNullOrEmpty(dto.getPageSize())
                    ? Constants.DEFAULT_PAGE_SIZE:dto.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = this.list(dto);
        return new PageInfo<T>(list);
    }
}
